﻿using System.Threading;
using System.Windows;
using System.Windows.Controls;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using ImageAppViewModels;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for BasicImageInfoView.xaml
    /// </summary>
    public partial class BasicImageInfoView : UserControl
    {
        public static ThreadSafeQueue<BasicImageInfoView> Queued = new ThreadSafeQueue<BasicImageInfoView>();
        public static Thread _t;

        static BasicImageInfoView()
        {            
        }

        public static void HandleBlurs()
        {
            while (true)
            {
                int maxConcurrentBlurs = Settings.Values.MaxConcurrentBlurCalcs;

                if (maxConcurrentBlurs == 0)
                    maxConcurrentBlurs = 2;

                while (Queued.Count == 0 || BackgroundCalcHandler.CurrentBlurs.Count > maxConcurrentBlurs)
                    Thread.Sleep(100);

                BasicImageInfoView vw;

                if (Queued.Dequeue(out vw))
                {
                    vw.CalcHandler.SetBackgroundCalcBlur(vw._vm);
                }

                Thread.Sleep(5);
            }
        }

        public BackgroundCalcHandler CalcHandler;
        public FileImageViewModel _vm;

        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register("FileImageViewModel", typeof(FileImageViewModel), typeof(BasicImageInfoView), new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { SetValue(FileImageViewModelProperty, value); }
        }

        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BasicImageInfoView source = d as BasicImageInfoView;
            source.UpdateFileImageViewModel(e.NewValue as FileImageViewModel, e.OldValue as FileImageViewModel);
        }

        public void UpdateFileImageViewModel(FileImageViewModel viewModel, FileImageViewModel viewModelOld)
        {
            FileImageViewModel = viewModel;
            _vm = viewModel;
            Queued.Clear();

            if (viewModelOld != null)
            {                
            }

            if (viewModel == null)
            {
                CalcHandler.Abort();
            }
            else
            {
                if (_t == null)
                {
                    ThreadStart ts = new ThreadStart(HandleBlurs);
                    _t = ts.CreateAndRunThread(true);
                    _t.Name = "HandleBlurs";
                }

                if (viewModel != viewModelOld)
                {
                    Queued.Enqueue(this);
                }
            }

            this.DataContext = FileImageViewModel;
        }

        public static readonly DependencyProperty ShowSimilarityGroupProperty =
            DependencyProperty.Register("ShowSimilarityGroup", typeof(bool), typeof(BasicImageInfoView), new PropertyMetadata(true, OnShowSimilarityGroupPropertyChanged));

        public bool ShowSimilarityGroup
        {
            get { return (bool)GetValue(ShowSimilarityGroupProperty); }
            set { SetValue(ShowSimilarityGroupProperty, value); }
        }

        private static void OnShowSimilarityGroupPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BasicImageInfoView source = d as BasicImageInfoView;
            source.UpdateShowSimilarityGroup((bool)e.NewValue);
        }

        public void UpdateShowSimilarityGroup(bool val)
        {
            ShowSimilarityGroup = val;

            if (val)
                simGrid.Visibility = Visibility.Visible;
            else
                simGrid.Visibility = Visibility.Collapsed;
        }

        public BasicImageInfoView()
        {
            InitializeComponent();
            CalcHandler = new BackgroundCalcHandler(true);
        }
    }
}
